package com.sxt.bus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxt.bus.domain.Apply;
import com.sxt.bus.domain.Goods;
import com.sxt.bus.domain.Provider;
import com.sxt.bus.service.ApplyService;
import com.sxt.bus.service.GoodsService;
import com.sxt.bus.service.ProviderService;
import com.sxt.bus.vo.ApplyVo;
import com.sxt.bus.vo.InportVo;
import com.sxt.sys.common.DataGridView;
import com.sxt.sys.common.ResultObj;
import com.sxt.sys.common.WebUtils;
import com.sxt.sys.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.xml.transform.Result;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 邵环
 * @since 2020-08-11
 */
@RestController
@RequestMapping("apply")
public class ApplyController {
    @Autowired
    private ApplyService applyService;

    @Autowired
    ProviderService providerService;

    @Autowired
    GoodsService goodsService;
    /**
     * 采购申请查询
     */
    @RequestMapping("loadAllApply")
    public DataGridView loadAllApply(ApplyVo applyVo) {
        //创建分页管理
        IPage<Apply> page = new Page<>(applyVo.getPage(), applyVo.getLimit());
        //创建条件构造器
        QueryWrapper<Apply> qw = new QueryWrapper<>();
        //如果供应商ID不为空并且不等于0，加入条件搜索
        qw.eq(applyVo.getProviderid()!=null&&applyVo.getProviderid()!=0,"providerid",applyVo.getProviderid());
        //如果商品ID不为空并且不等于0，加入条件搜索
        qw.eq(applyVo.getGoodsid()!=null&&applyVo.getGoodsid()!=0,"goodsid",applyVo.getGoodsid());
        //如果审核条件不等于空，加入条件搜索
        qw.like(applyVo.getTocheck()!=null,"tocheck",applyVo.getTocheck());
        //如果备注不等于空，加入条件搜索
        qw.like(applyVo.getRemark()!=null,"remark",applyVo.getRemark());
        //如果开始时间不为空，加入条件搜索比这个时间大的
        qw.ge(applyVo.getStartTime()!=null,"createtime",applyVo.getStartTime());
        //如果截止时间不为空，加入条件搜索比这个时间小的
        qw.le(applyVo.getEndTime()!=null,"createtime",applyVo.getEndTime());
        //如果单号不为空，加入条件搜索
        qw.like(applyVo.getCode()!=null,"code",applyVo.getCode());
        //从大到小排序，ID为条件
        qw.orderByDesc("id");
        //存入page和条件构造器
        this.applyService.page(page,qw);
        //得到结果
        List<Apply> records = page.getRecords();
        //循环遍历结果值
        for (Apply apply : records) {
            //根据结果值的供应商ID查找对应的供应商
            Provider provider = this.providerService.getById(apply.getProviderid());
            if(null!=provider){
                //如果供应商不为空，则将供应商名称加入订单
                apply.setProvidername(provider.getProvidername());
            }
            //根据结果值的商品ID查找对应的商品
            Goods goods = this.goodsService.getById(apply.getGoodsid());
            if(goods!=null){
                //如果商品不为空，则将商品名称和规格加入订单
                apply.setGoodsname(goods.getGoodsname());
                apply.setSize(goods.getSize());
            }
        }
        return new DataGridView(page.getTotal(),records);
    }

    /**
     * 添加
     */
    @RequestMapping("addApply")
    public ResultObj addApply(ApplyVo applyVo) {
        try {
            //订单创建时间
            applyVo.setCreatetime(new Date());
            //获得Session中的用户名
            User user=(User) WebUtils.getSession().getAttribute("user");
            applyVo.setOperateperson(user.getName());

            //创建一个200811格式的时间
            String code = new SimpleDateFormat("yyHHmm").format(new Date());
            //获取最新的单号
            String maxCode = applyService.getMaxCode(code);
            //如果获取的最新时间不为空，则截取
            if(maxCode!=null && !"".equals(maxCode)){
                //截取后面的时间
                int i1 = Integer.parseInt(maxCode.substring(2));
                //将截取后的时间+1并拼接好
                code = "A"+(i1+1);
            }else{//如果为空则直接插入订单号初始值
                code = "A"+code + "001";
            }
            applyVo.setCode(code);
            //applyVo.setOperateperson(user.getName());
            this.applyService.save(applyVo);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 修改
     */
    @RequestMapping("updateApply")
    public ResultObj updateApply(ApplyVo applyVo) {
        try {
            this.applyService.updateById(applyVo);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 删除
     */
    @RequestMapping("deleteApply")
    public ResultObj deleteApply(Integer id) {
        try {
            this.applyService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }
    /**
     * 审核
     */
    @RequestMapping("checkApply")
    public ResultObj checkApply(Integer id){
        try {
            Apply apply = this.applyService.getById(id);
            if(apply.getTocheck()=="Y"){
                return ResultObj.ERROR_ERROR;
            }
            apply.setTocheck("Y");
            apply.setChecktime(new Date());
            this.applyService.updateById(apply);
            return ResultObj.CHECK_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.CHECK_ERROR;
        }
    }

    /**
     * 反审
     */
    @RequestMapping("checkOutApply")
    public ResultObj checkOutApply(Integer id){
        try {
            Apply apply = this.applyService.getById(id);
            if(apply.getTocheck()=="N"){
                return ResultObj.ERROR_ERROR;
            }
            apply.setTocheck("N");
            this.applyService.updateById(apply);
            return ResultObj.CHECK_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.CHECK_ERROR;
        }
    }
}

